/**
 * Copyright (c) 2024 - CARThunder
 * All Rights Reserved by Chengdu CARThunder Intelligent Technology Co., LTD and its affiliates.
 * You may not use, copy, distribute, modify, transmit in any form this file
 * except in compliance with CARThunder in writing by applicable law.
 *
 */

/**
 * file description.
 * @file    filename
 * @brief   brief function description.
 * @details detailed function description.
 * @version 1.0
 * @author  author_name
 * @date    last_modified_date
 *
 *      Edit History
 *      ----------------------------------------------------------------------------
 *      DATE                     NAME               DESCRIPTION
 *      2024-08-2               jia fengyi         Create it.
 *
 */

package com.cart.musicdemo.base.utils;

import android.content.Context;
import android.content.SharedPreferences;

public class PreferencesUtil {
    private static final String PREFS_NAME = "UsbMusicPlayerPrefs";
    private static final String KEY_PLAYBACK_POSITION = "PlayBackPosition";
    private static final String KEY_PLAYBACK_PATH = "PlayBackPath";
    private static final String KEY_PLAYBACK_DURATION = "PlayBackDuration";

    public static void savePlaybackPosition(Context context, int position) {
        SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putInt(KEY_PLAYBACK_POSITION, position);
        editor.apply(); // or editor.commit()
    }

    public static int getPlaybackPosition(Context context) {
        SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        return prefs.getInt(KEY_PLAYBACK_POSITION, 0); // Default to 0 if not found
    }

    public static void savePlaybackPath(Context context, String path) {
        SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString(KEY_PLAYBACK_PATH, path);
        editor.apply(); // or editor.commit()
    }

    public static String getPlaybackPath(Context context) {
        SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        return prefs.getString(KEY_PLAYBACK_PATH, ""); // Default to empty string if not found
    }

    // 保存duration的方法
    public static void savePlayBackDuration(Context context, int duration) {
        SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putInt(KEY_PLAYBACK_DURATION, duration);
        editor.apply(); // or editor.commit()
    }

    // 获取duration的方法
    public static int getPlayBackDuration(Context context) {
        SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        return prefs.getInt(KEY_PLAYBACK_DURATION, 0); // Default to 0 if not found
    }
}
